package com.ml.mall.conversion;

import com.ml.mall.bean.req.BaseDateRangeReq;
import com.ml.mall.bean.req.admin.purchase.PurchaseQueryOneReq;
import com.ml.mall.bean.res.admin.AdminOverviewRes;
import com.ml.mall.bean.res.admin.AdminPurchaseRes;
import com.ml.mall.bean.res.admin.PurchaseQueryOneRes;
import com.ml.mall.bean.res.admin.vo.*;
import com.ml.mall.bean.vo.DateValueVO;
import com.ml.mall.utils.DateTransformUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

/**
 * Created by kyyang on 2021/6/9 23:54
 */

public class AnalyticsPartnerServiceHelp {
    



    public static AdminPurchaseRes adminPurchaseOrderArrConversion(BaseDateRangeReq req, Future<List<DateValueVO>> purchaseOrderArrFuture, Future<List<DateValueVO>> purchaseSpentArrFuture) throws ExecutionException, InterruptedException {
        AdminPurchaseRes res = new AdminPurchaseRes();
        if(purchaseOrderArrFuture!=null){
            List<PurchaseOrderArrVO> purchaseOrderArr = new ArrayList<>();
            List<DateValueVO> dateValueVOS = purchaseOrderArrFuture.get();
            if(dateValueVOS!=null&&dateValueVOS.size()>0){
                Map<String, List<DateValueVO>> collect = dateValueVOS.stream().collect(Collectors.groupingBy(DateValueVO::getName));
                for(Map.Entry<String, List<DateValueVO>> entry: collect.entrySet()){
                    PurchaseOrderArrVO purchaseOrderArrVO = new PurchaseOrderArrVO();
                    String key = entry.getKey();
                    List<DateValueVO> value = entry.getValue();
                    purchaseOrderArrVO.setPurchaserName(key);
                    Long v = value.stream().collect(Collectors.summingLong(e -> e.getValue().longValue()));
                    purchaseOrderArrVO.setTotalPo(v);
                    Map<String, Double> result = value.stream().collect(Collectors.groupingBy(e -> e.getYmd(), Collectors.summingDouble(g -> g.getValue().doubleValue())));
                    List<DateValueVO> curDateMonthDataInit = DateTransformUtils.getCurDateMonthDataInit(req.getTimeBegin(), req.getTimeEnd(), result);
                    purchaseOrderArrVO.setDayDetail(curDateMonthDataInit);
                    purchaseOrderArr.add(purchaseOrderArrVO);
                }
            }
            res.setPurchaseOrderArr(purchaseOrderArr);
        }
        if(purchaseSpentArrFuture!=null){
            List<PurchaseSpentReportVO> purchaseSpentReportVOS = new ArrayList<>();
            List<DateValueVO> dateValueVOS = purchaseOrderArrFuture.get();
            if(dateValueVOS!=null&&dateValueVOS.size()>0){
                Map<String, List<DateValueVO>> collect = dateValueVOS.stream().collect(Collectors.groupingBy(DateValueVO::getName));
                for(Map.Entry<String, List<DateValueVO>> entry: collect.entrySet()){
                    PurchaseSpentReportVO purchaseSpentReportVO = new PurchaseSpentReportVO();
                    String key = entry.getKey();
                    List<DateValueVO> value = entry.getValue();
                    purchaseSpentReportVO.setCompanyName(key);
                    Double v = value.stream().collect(Collectors.summingDouble(e->e.getValue().doubleValue()));
                    purchaseSpentReportVO.setTotalSpent(BigDecimal.valueOf(v));
                    Map<String, Double> result = value.stream().collect(Collectors.groupingBy(e -> e.getYmd(), Collectors.summingDouble(g -> g.getValue().doubleValue())));
                    List<DateValueVO> curDateMonthDataInit = DateTransformUtils.getCurDateMonthDataInit(req.getTimeBegin(), req.getTimeEnd(), result);
                    purchaseSpentReportVO.setDayDetail(curDateMonthDataInit);
                    purchaseSpentReportVOS.add(purchaseSpentReportVO);
                }
            }
            res.setPurchaseSpentReport(purchaseSpentReportVOS);
        }
        return res;
    }

}
